package com.justech.eat.service.component.impl;

import com.alibaba.fastjson.JSONObject;
import com.justech.eat.service.component.UserTokenService;
import com.justech.eat.util.RSAUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * @program: eat-service
 * @description:
 * @author: feifei.li
 * @create: 2020-06-15 15:50
 **/
@Component
public class NativeUserTokenService implements UserTokenService {
    @Value("${rsa.privateKey}")
    private String rsaPrivateKey;

    @Value("${expirTime}")
    private int expireTime;

    @Value("${aes.password}")
    private String aesPassword;

    @Value("${aes.ivp}")
    private String aesIpv;

    @Value("${aes.algorithmStr}")
    private String aesAlgorithmStr;

    /**
     * 获取用户信息
     *
     * @param token 已加密token
     * @return
     */
    @Override
    public String getJobNumber(String token) {
        String user = null;
        try {
            user = RSAUtil.rsaDecrypt(token, rsaPrivateKey);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        if (user == null) {
            throw new RuntimeException(NATIVE + "解析失败");
        }
        JSONObject object = JSONObject.parseObject(user);
        return object.getString("empNo");
    }
}
